using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DeSleeper.Service
{
    public class HostInfo : IXmlSerializable, IEquatable<HostInfo>
    {
        public IPAddress Current { get; set; }
        public IPAddress Subnet { get; set; }
        public PhysicalAddress MacAddress { get; set; }

    	XmlSchema IXmlSerializable.GetSchema()
    	{
    		return null;
    	}

    	void IXmlSerializable.ReadXml(XmlReader reader)
		{
			reader.ReadStartElement();
			Current = IPAddress.Parse(reader.ReadElementString("Current"));
			Subnet = IPAddress.Parse(reader.ReadElementString("Subnet"));
			MacAddress = PhysicalAddress.Parse(reader.ReadElementString("MacAddress"));
			reader.ReadEndElement();
    	}

    	void IXmlSerializable.WriteXml(XmlWriter writer)
    	{
			writer.WriteElementString("Current", Current.ToString());
			writer.WriteElementString("Subnet", Subnet.ToString());
			writer.WriteElementString("MacAddress", MacAddress.ToString());
    	}

    	public bool Equals(HostInfo other)
    	{
    		if (ReferenceEquals(null, other)) return false;
    		if (ReferenceEquals(this, other)) return true;
    		return Equals(other.Current, Current) && Equals(other.Subnet, Subnet) && Equals(other.MacAddress, MacAddress);
    	}

    	public override bool Equals(object obj)
    	{
    		if (ReferenceEquals(null, obj)) return false;
    		if (ReferenceEquals(this, obj)) return true;
    		if (obj.GetType() != typeof (HostInfo)) return false;
    		return Equals((HostInfo) obj);
    	}

    	public override int GetHashCode()
    	{
    		unchecked
    		{
    			int result = (Current != null ? Current.GetHashCode() : 0);
    			result = (result*397) ^ (Subnet != null ? Subnet.GetHashCode() : 0);
    			result = (result*397) ^ (MacAddress != null ? MacAddress.GetHashCode() : 0);
    			return result;
    		}
    	}
    }
}